﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Entity;

namespace DAO
{
    public class QuanLyLoaiSPDAO
    {
        public static DataTable LayThongTinLoaiSP()
        {
            SqlConnection connect = null;
            DataTable rs = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }
                String cmdText = "SELECT * FROM LOAI_SAN_PHAM";
                rs = SqlHelper.ExecuteDataset(connect, CommandType.Text, cmdText).Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (rs != null)
                    ((IDisposable)rs).Dispose();

                if (connect != null)
                    connect.Dispose();
            }
            return rs;
        }

        public static void SuaLoaiSP(LoaiSanPham loaisp)
        {
            SqlConnection connect = null;
            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[2];
                param[0] = new SqlParameter("@maloaisp", loaisp.maloaisp);
                param[1] = new SqlParameter("@tenloaisp", loaisp.tenloaisp);
                String cmdText = "UPDATE LOAI_SAN_PHAM SET TenLoaiSP = @tenloaisp WHERE MaLoaiSP = @maloaisp";
                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, param);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void XoaLoaiSP(String maloaisp)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@maloaisp", maloaisp);
                String cmdText = "DELETE LOAI_SAN_PHAM WHERE MaLoaiSP = @maloaisp";
                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void ThemLoaiSP(LoaiSanPham loaisp)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@maloaisp", loaisp.maloaisp);
                par[1] = new SqlParameter("@tenloaisp", loaisp.tenloaisp);
                String cmdText = "INSERT INTO LOAI_SAN_PHAM VALUES (@maloaisp,@tenloaisp)";

                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }
    }
}
